var map_height2d : int = 50;
var map_width2d : int = 50;
var scale : float = 1.0;
var number_of_rooms : int = 100;
var default_template : GameObject;
var open_template : GameObject;
var tee_template : GameObject;
var straight_template : GameObject;
var corner_template : GameObject;
var end_template : GameObject;
var parent_of_tiles : GameObject;
private var current_room_number : int = 1;


function Start (){
    // loads a map

    print("Starting map generator...");
    // create array
    var map2d = new Array();
    
    // make it an array
    for (x = 0; x < map_width2d + 1; x++){ // loop through
        
        map2d.push(new Array()); // add a new column
        for (y = 0; y < map_height2d + 1; y++){
            map2d[x][y] = 0; // set all points to 0
        }
    }
    
    var x_mess = 0;
    var y_mess = 0;
    // loop through every room we need to make
    //var number_of_rooms = 100;
    // set position, example: [20,20]
    var current_position = new Array();
    current_position[0] = map_width2d / 2;
    current_position[1] = map_height2d / 2;
    
    
    if (number_of_rooms > map_height2d * map_width2d){
    print('...too many rooms for specified map size.');
    }
    else{
    
        while(current_room_number < number_of_rooms){
            current_room_number++;
            
            
            random_value = Random.value;
            // get a random number between 0 and 4
            random_value_in_range = random_value * 4;
            // 0 = right, 1 = up, 2 = left, 3 = down
            integer_direction = Mathf.Floor(random_value_in_range);
            if (integer_direction == 0){
                // we move right
                if (current_position[0] < map_width2d - 2){
                    current_position[0] += 1;
                }
                else{
                    current_room_number--;
                }
            }
        
            if (integer_direction == 1){
                // we move up
                if (current_position[1] > 2){
                    current_position[1] -= 1;
                }
                else{
                    current_room_number--;
                }
            }
        
            if (integer_direction == 2){
                // we move left
                if (current_position[0] > 2){
                    current_position[0] -= 1;
                }
                else{
                        current_room_number--;
                }
            }
        
            if (integer_direction == 3){
                // we move down
                if (current_position[1] < map_height2d - 2){
                    current_position[1] += 1;
                }
                else{
                    current_room_number--;
                }
            }

            // if the current is already filled
            if (map2d[current_position[0]][current_position[1]]  == 1){
                current_room_number--;
                }
            map2d[current_position[0]][current_position[1]] = 1;
	
    
        }
        print('...map file created!');
        print('Loading cell objects...');
        // output the map
        
		//Places FPS Player
		if(GameObject.Find("Player")){
			var player : GameObject = GameObject.Find("Player");
			player.transform.position = Vector3((current_position[1] - map_height2d / 2) * scale, 1, (current_position[0] - map_width2d / 2) * scale);
		}
	
		//Places the Tiles
		for (x = 0; x < map_width2d - 1; x++){
            for (y = 0; y < map_height2d - 1; y++){
                if (map2d[x][y] == 1){
					var room = Room_Placer(map2d, x, y);
					room.transform.parent = parent_of_tiles.transform;
					}
				}
            }        
        }
		
		//Merges the Tiles to a single Mesh
		for (var child in parent_of_tiles.transform){
			child.position += parent_of_tiles.transform.position;
		}
		parent_of_tiles.transform.position = Vector3.zero;
		parent_of_tiles.transform.rotation = Quaternion.identity;
		
		var meshFilters = parent_of_tiles.GetComponentsInChildren(MeshFilter);
		var combine : CombineInstance[] = new CombineInstance[meshFilters.Length];
		var index : int = 0;
		for( i = 0; i < meshFilters.Length; i++){
			combine[index].mesh = meshFilters[i].sharedMesh;
			combine[index].transform = meshFilters[i].transform.localToWorldMatrix;
			index++;
		}
		parent_of_tiles.GetComponent(MeshFilter).mesh = new Mesh();
		parent_of_tiles.GetComponent(MeshFilter).mesh.CombineMeshes(combine, true);
        
		
		
		print('...cells loaded!');

}
function Room_Placer(map2d, x ,y) {
	position = Vector3((y - map_height2d / 2) * scale, 0, (x - map_width2d / 2) * scale);
	var room : Object;
	//4 open sides
	
	if (map2d[x+1][y] == 1 && map2d[x -1][y] == 1 && map2d[x][y+1] == 1 && map2d[x][y-1] == 1 ){
		room = Instantiate(open_template,position, Quaternion.identity);
		return room;
	}
	
	//3 open sides  180
	else if(map2d[x+1][y] == 1 && map2d[x-1][y] == 1 && map2d[x][y+1] ){
		room= Instantiate(tee_template,position, Quaternion.identity);
		room.transform.eulerAngles.y = 180;
		return room;
		
	}
		
	//3 open sides  0
	if (map2d[x+1][y] == 1 && map2d[x -1][y] == 1 && map2d[x][y-1] ){
		room= Instantiate(tee_template,position, Quaternion.identity);
		return room;
	}
	
	//3 open sides 90
	if (map2d[x+1][y] == 1 && map2d[x][y+1] && map2d[x][y-1] ){
		room= Instantiate(tee_template,position, Quaternion.identity);
		room.transform.eulerAngles.y = 90;
		return room;
	}
	
	//3 open sides  -90
	if (map2d[x -1][y] == 1 && map2d[x][y+1] && map2d[x][y-1] ){
		room= Instantiate(tee_template,position, Quaternion.identity);
		room.transform.eulerAngles.y = -90;
		return room;
	}
	
	//2 open straight 0
	if (map2d[x+1][y] == 1 && map2d[x -1][y] == 1){
		room = Instantiate(straight_template,position, Quaternion.identity);
		return room;
	}
	
	//2 open straight 90
	if (map2d[x][y+1] == 1 && map2d[x][y-1] == 1 ){
		room = Instantiate(straight_template,position, Quaternion.identity);
		room.transform.eulerAngles.y = 90;
		return room;
	}
	
	//2 open corner 90
	if (map2d[x+1][y] == 1 && map2d[x][y+1] == 1){
		room = Instantiate(corner_template,position, Quaternion.identity);
		room.transform.eulerAngles.y = 90;
		return room;
	}
	
	//2 open corner 0
	if (map2d[x+1][y] == 1 && map2d[x][y-1] == 1 ){
		room = Instantiate(corner_template,position, Quaternion.identity);
		return room;
	}
	
	//2 open corner -90
	if (map2d[x -1][y] == 1 && map2d[x][y+1] == 1){
		room = Instantiate(corner_template,position, Quaternion.identity);
		room.transform.eulerAngles.y = 180;
		return room;
	}
	
	 //2 open corner 180
	if (map2d[x -1][y] == 1 && map2d[x][y-1] == 1){
		room = Instantiate(corner_template , position, Quaternion.identity);
		room.transform.eulerAngles.y = -90;
		return room;
	}
	
	// 1 open end 90
	else if(map2d[x+1][y] == 1){
		room= Instantiate(end_template,position, Quaternion.identity);
		room.transform.eulerAngles.y = 90;
		return room;
		
	} 
	
	//1 open end -90
	else if(map2d[x-1][y] == 1){
		room= Instantiate(end_template,position, Quaternion.identity);
		room.transform.eulerAngles.y = -90;
		return room;
	} 
	
	//1 open end 180
	else if(map2d[x][y+1] == 1){
		room= Instantiate(end_template,position, Quaternion.identity);
		room.transform.eulerAngles.y = 180;
		return room;
	} 
	
	//1 open end 0
	else if(map2d[x][y-1] == 1){
		room= Instantiate(end_template,position, Quaternion.identity);
		return room;
	} 
	
	else{
		return Instantiate(default_template,position,Quaternion.identity);
	}
	
	
}

function Update(){

}